/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

// import { Alert, Button } from 'antd';
import { pickedFaceData } from '@amcax/base';
import { previewer } from '@app-cad/module';
import { PickInputRJSFFormat } from '@app-cad/common/components/command-form/constants';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { useTools } from '@app-cad/common/hooks/useTools';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useEffect, useState } from 'react';

type Params = {
  profile: string[];
};

function CmdMsolid({ title, onClose, props }: CurvedCmdDialogParams) {
  const [formData, setFormData] = useState<pickedFaceData[]>([]);

  const schema: RJSFSchema = {
    type: 'array',
    items: {
      type: 'string',
      enum: [],
    },
    format: PickInputRJSFFormat.MultiPickFace,
    uniqueItems: true,
  };

  const { picker } = useTools();

  useEffect(() => {
    preview();
  }, [formData]);

  useAsyncEffect(async () => {
    if (props != null) {
      const params: Params = props.params;
      const { profile } = params;

      await initEditDependency(profile);

      setFormData(
        profile.map(
          (face) => picker.generatePickObjByPickID(face) as pickedFaceData,
        ),
      );
    }
  }, []);

  const isFormDataValid = () =>
    formData.length > 0 && formData.every((c) => c != null);

  const checkAndFinishDraw = async () => {
    if (!isFormDataValid()) {
      return;
    }

    await execute();
    onClose();
  };

  const { initEditDependency, initingEdit, confirmCommand, isLoading } =
    useCommand('cmd-msolid', {
      onClose,
      finishDraw: checkAndFinishDraw,
      editDxid: props?.shape,
      commandName: 'CreateMsolid',
    });

  const execute = async (preview = false) => {
    const params = {
      profile: formData.map((i) => i.meshId),
    };

    await confirmCommand(
      {
        a: params,
      },
      preview,
    );
  };

  const preview = async () => {
    if (!isFormDataValid()) {
      previewer.clear();
      return;
    }

    await execute(true);
  };

  return (
    <PickableCommandForm
      disabled={initingEdit}
      onClose={onClose}
      title={title}
      schema={schema}
      submitLoading={isLoading}
      onChange={(v) => {
        setFormData(v);
      }}
      submitText='完成'
      onSubmit={checkAndFinishDraw}
      extraUiSchema={{
        'ui:autofocus': true,
        'ui:placeholder': '请输入边界曲面',
      }}
      formData={formData}
      noValidate
      isFormDataValid={isFormDataValid()}
    />
  );
}

export const cmdMsolid: CurvedBaseCommand = {
  id: 'CmdMsolid',
  label: 'command.msolid',
  icon: 'commands/feature/msolid',
  help: {
    remark: 'tips.redo',
  },
  create: (label, onClose, props) => (
    <CmdMsolid
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
